<template>
	<view class="content padding">
		<view class="priceBox">
			<view class="color_ling text_g">
				实付金额
			</view>
			<view class="margin_top">
				<text class="price">￥{{payprice}}</text>
			</view>
		</view>
		<view class="margin_top padding ffffff border">
			<view class="color_san text_g">
				选择支付方式
			</view>
			<!-- 微信 -->
			<view class="paddingtb border_bottom flexbetween" @click="pay_type=1">
				<view class="flexleft">
					<image class="margin_right3 imgBox" src="/static/image/home/weixin.png" mode="widthFix"></image>
					<view class="color_san text_a">
						微信
					</view>
				</view>
				<view class="">
					<view class="xuanzhongs" v-if="pay_type==1"></view>
					<view class="wiexuanzhongs" v-if="pay_type!=1"></view>
				</view>
			</view>
			<!-- 支付宝 -->
			<view class="paddingtb border_bottom flexbetween" @click="pay_type=2">
				<view class="flexleft">
					<image class="margin_right3 imgBox" src="/static/image/home/zhifubao.png" mode="widthFix"></image>
					<view class="color_san text_a">
						支付宝
					</view>
				</view>
				<view class="">
					<view class="xuanzhongs" v-if="pay_type==2"></view>
					<view class="wiexuanzhongs" v-if="pay_type!=2"></view>
				</view>
			</view>
		</view>
		<!-- 立即支付 -->
		<view class="footer padding">
			<view class="footerButton" @click="payOrder">
				立即支付
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				pay_type: 1,
				dataAll: {
					address_id: "",
					cart_ids: "",
					from_type: "",
					goods_num: "",
					goods_id: "",
					goods_sku_unique: "",
					is_alone: "",
					goods_group_buying_id: "",
					remarks: ""
				},
				payprice: "",
				wxPayMsg: {},
				zfbMsg: "",
				type: 1, //1普通支付，2订单列表支付，3订单全款购买，4服务订单，5服务订单列表支付，6解锁视频支付
				order_id: "",
				community_id: "", //视频id
				is_submit:true,
			}
		},
		onLoad(options) {
			if (options.msg) { //普通支付
				let data = JSON.parse(decodeURIComponent(options.msg))
				this.payprice = data.total_price
				delete data.total_price
				this.dataAll = data
				this.type = 1
			}
			if (options.msg1) { //订单列表支付
				let data = JSON.parse(decodeURIComponent(options.msg1))
				this.payprice = data.total_price
				this.order_id = data.order_id
				this.type = 2
			}
			if (options.msg2) { //订单全款购买
				let data = JSON.parse(decodeURIComponent(options.msg2))
				this.order_id = data.order_id
				this.init()
				this.type = 3
			}
			if (options.msg3) { //服务订单
				let data = JSON.parse(decodeURIComponent(options.msg3))
				console.log(data, "999");
				this.payprice = data.total_price
				delete data.total_price
				this.dataAll = data
				this.type = 4
			}
			if (options.msg4) { //服务订单列表支付
				let data = JSON.parse(decodeURIComponent(options.msg4))
				this.payprice = data.total_price
				this.order_id = data.order_id
				this.type = 5
			}
			if (options.msg5) { //解锁视频支付
				let data = JSON.parse(decodeURIComponent(options.msg5))
				this.payprice = data.price
				this.community_id = data.id
				this.type = 6
			}
			if (options.msg6) { //打赏支付
				let data = JSON.parse(decodeURIComponent(options.msg6))
				console.log(data)
				this.payprice = data.pay_price
				this.dataAll = data
				// this.community_id = data.id
				this.type = 7
			}
		},
		methods: {
			init() {
				httpRequest.request('/api/goods_order/buyfull_pre', 'POST', {
					order_id: this.order_id,
				}, false, false, true).then(res => {
					this.payprice = res.data.pay_price
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			payOrder() {
				if(!this.is_submit){
					return false;
				}
				this.is_submit=false;
				let url = ""
				let data = {
					order_id: this.order_id,
					pay_type: this.pay_type,
				}
				if (this.type == 1) { //普通支付
					data = this.dataAll
					this.$set(data, "pay_type", this.pay_type)
					url = "/api/goods_order/create_order"
				} else if (this.type == 2) { //订单列表支付
					url = "/api/goods_order/gotopay"
				} else if (this.type == 3) { //订单全款购买
					url = "/api/goods_order/buyfull"
				} else if (this.type == 4) { //服务订单
					data = this.dataAll
					this.$set(data, "pay_type", this.pay_type)
					url = "/api/service_order/create_order"
				} else if (this.type == 5) { //服务订单列表支付
					url = "/api/service_order/gotopay"
				} else if (this.type == 6) { //解锁视频支付
					data = {
						community_id: this.community_id,
						pay_type: this.pay_type
					}
					url = "/api/community/create_video_order"
				} else if (this.type == 7) {
					data = this.dataAll
					this.$set(data, "pay_type", this.pay_type)
					url = "/api/community/create_reward_order"
				}
				console.log(data);
				httpRequest.request(url, 'POST', data, false, false, true).then(res => {
					if (res.code == 1) {
						if (Number(this.payprice) == 0) {
							httpRequest.toast("支付成功")
							setTimeout(() => {
								uni.navigateBack(1)
							}, 500)
							return false
						}
						if (this.pay_type == 1) { //微信支付
							// return false
							this.wxPayMsg = res.data.wxconfig
							console.log(JSON.stringify(this.wxPayMsg))
							this.wxPay()
						} else { //支付宝支付
							this.zfbMsg = res.data.aliconfig
							this.zfbPay()
						}
					} else {
						httpRequest.toast(res.msg);
						this.is_submit=true;
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
					this.is_submit=true;
				})
			},
			wxPay() {
				// console.log(JSON.stringify(this.wxPayMsg))
				// return false
				let _this = this
				uni.requestPayment({
					provider: 'wxpay',
					orderInfo: _this.wxPayMsg, //后台返回的支付数据
					success: function(res) {
						setTimeout(() => {
							if (_this.type == 1) { //普通支付
							
								uni.redirectTo({
									url: "/pages_mine/businessOrder?value=0"
								})
							} else if (_this.type == 2) { //订单列表支付
								uni.navigateBack(1)
							} else if (_this.type == 3) { //订单全款购买
								uni.redirectTo({
									url: "/pages_mine/businessOrder?value=0"
								})
							} else if (_this.type == 4) { //服务订单
								
								uni.redirectTo({
									url: "/pages_mine/serviceOrder"
								})
							} else if (_this.type == 5) { //服务订单列表支付
								uni.navigateBack(1)
							} else if (_this.type == 6) { //解锁视频支付
								
								uni.navigateBack(1)
							} else if (_this.type == 7) {
								
								uni.navigateBack(1)
							}
						}, 500)
					},
					fail: function(err) {
						if (_this.type == 1) { //普通支付
						
							uni.redirectTo({
								url: "/pages_mine/businessOrder?value=0"
							})
						} else if (_this.type == 2) { //订单列表支付
							uni.navigateBack(1)
						} else if (_this.type == 3) { //订单全款购买
							uni.redirectTo({
								url: "/pages_mine/businessOrder?value=0"
							})
						} else if (_this.type == 4) { //服务订单
							
							uni.redirectTo({
								url: "/pages_mine/serviceOrder"
							})
						} else if (_this.type == 5) { //服务订单列表支付
							uni.navigateBack(1)
						} else if (_this.type == 6) { //解锁视频支付
							
							uni.navigateBack(1)
						} else if (_this.type == 7) {
							
							uni.navigateBack(1)
						}
					}
				});
			},
			zfbPay() {
				let _this = this
				console.log(_this.zfbMsg);
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: _this.zfbMsg, //后台返回的支付数据
					success: function(res) {
						console.log(res, 99999999)
						setTimeout(() => {
							if (this.type == 1) { //普通支付
							
								uni.redirectTo({
									url: "/pages_mine/businessOrder?value=0"
								})
							} else if (this.type == 2) { //订单列表支付
								uni.navigateBack(1)
							} else if (this.type == 3) { //订单全款购买
								uni.redirectTo({
									url: "/pages_mine/businessOrder?value=0"
								})
							} else if (this.type == 4) { //服务订单
								
								uni.redirectTo({
									url: "/pages_mine/serviceOrder"
								})
							} else if (this.type == 5) { //服务订单列表支付
								uni.navigateBack(1)
							} else if (this.type == 6) { //解锁视频支付
								
								uni.navigateBack(1)
							} else if (this.type == 7) {
								
								uni.navigateBack(1)
							}
							
						}, 500)
					},
					fail: function(err) {
						if (this.type == 1) { //普通支付
						
							uni.redirectTo({
								url: "/pages_mine/businessOrder?value=0"
							})
						} else if (this.type == 2) { //订单列表支付
							uni.navigateBack(1)
						} else if (this.type == 3) { //订单全款购买
							uni.redirectTo({
								url: "/pages_mine/businessOrder?value=0"
							})
						} else if (this.type == 4) { //服务订单
							
							uni.redirectTo({
								url: "/pages_mine/serviceOrder"
							})
						} else if (this.type == 5) { //服务订单列表支付
							uni.navigateBack(1)
						} else if (this.type == 6) { //解锁视频支付
							
							uni.navigateBack(1)
						} else if (this.type == 7) {
							
							uni.navigateBack(1)
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.priceBox {
		display: flex;
		flex-direction: column;
		align-items: center;

		.price {
			font-size: 71rpx;
			font-family: DIN-Bold, DIN;
			font-weight: bold;
			color: #FF0000;
		}
	}

	.border_bottom {
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.05);
		// border-bottom: 2rpx solid red;
	}

	.imgBox {
		width: 42rpx;
		height: 42rpx;
	}
</style>